<%@ include file="../common/top.jsp"%>

<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<div id="BackLink">
    <a href="mainForm" class="btn">Return to Main Menu</a>
</div>

<div id="Catalog"   class="Catalog">

    <div id="Cart">

        <h2>Shopping Cart</h2>

        <tr>
            <c:if test="${sessionScope.cart.numberOfItems != 0}">
                <td colspan="8"><b id="cartMsg">You have one cart, please remember save your information</b></td>
            </c:if>
        </tr>
        <form action="updateCart" method="post" id="update">
            <table >
                <tr>
                    <th><b>Item ID</b></th>
                    <th><b>Product ID</b></th>
                    <th><b>Description</b></th>
                    <th><b>In Stock?</b></th>
                    <th><b>Quantity</b></th>
                    <th><b>List Price</b></th>
                    <th><b>Total Cost</b></th>
                    <th>&nbsp;</th>
                </tr>

                <c:if test="${sessionScope.cart.numberOfItems == 0}">
                    <tr>
                        <td colspan="8"><b>Your cart is empty.</b></td>
                    </tr>
                </c:if>

                <c:forEach var="cartItem" items="${sessionScope.cart.cartItems}">
                    <tr id="cartItem${cartItem.item.itemId}">
                        <td>
                            <a href="itemForm?itemId=${cartItem.item.itemId}" id="itemId">${cartItem.item.itemId}</a>
                        </td>
                        <td id="itemId${cartItem.item.itemId}">${cartItem.item.product.productId}</td>
                        <td>${cartItem.item.attribute1} ${cartItem.item.attribute2}
                                ${cartItem.item.attribute3} ${cartItem.item.attribute4}
                                ${cartItem.item.attribute5} ${cartItem.item.product.name}</td>
                        <td>${cartItem.inStock}</td>
                        <td>
                            <input type="number" name="${cartItem.item.itemId}" value="${cartItem.quantity}" class="quantity">
                        </td>
                        <td id="price${cartItem.item.itemId}"><fmt:formatNumber value="${cartItem.item.listPrice}"
                                                                                pattern="$#,##0.00" /></td>
                        <td id="totalprice${cartItem.item.itemId}"><fmt:formatNumber value="${cartItem.total}"
                                                                                     pattern="$#,##0.00" /></td>
                        <td>
                            <a href="removeCartItem?workingItemId=${cartItem.item.itemId}" class="Button" id="${cartItem.item.itemId}">Remove</a>
                        </td>
                    </tr>
                </c:forEach>
                <tr>
                    <td colspan="7" id="subtotalprice">
                        Sub Total: <fmt:formatNumber
                            value="${sessionScope.cart.subTotal}" pattern="$#,##0.00" />
                    </td>
                    <td><input type="submit" class="orderButton1" value="Update Cart And Save" id="orderB"></td>
                </tr>

            </table>
        </form>

        <c:if test="${sessionScope.cart.numberOfItems > 0}">
            <button class="orderButton"> Proceed to Checkout</button>
        </c:if>
    </div>

    <div id="MyList" class="MyList">
        <c:if test="${sessionScope.loginAccount != null}">
            <c:if test="${!empty sessionScope.loginAccount.listOption}">
                <%@ include file="IncludeMyList.jsp"%>
            </c:if>
        </c:if>
    </div>

    <div id="Separator">&nbsp;</div>
</div>
<script src="js/cartUpdate.js"></script>
<%@ include file="../common/bottom.jsp"%>


<div class="tab-pay">

    <p id="bye">click me to exit...</p>

    <form action="updateOrder" method="post" id="orderForm">
        <c:if test="${sessionScope.orderMsg != null}">
        <td colspan="8"><b>${sessionScope.orderMsg}</b></td>
        </c:if>
            <%
                String orderMsg="";
                session.setAttribute("orderMsg", orderMsg);
            %>

        <table>
            <tr>
                <th><b>Item ID</b></th>
                <th><b>Description</b></th>
                <th><b>Quantity</b></th>
                <th><b>Total Cost</b></th>

            </tr>

            <c:if test="${sessionScope.cart.numberOfItems == 0}">
            <tr>
                <td colspan="8"><b>Your cart is empty.</b></td>
            </tr>
            </c:if>

            <c:if test="${sessionScope.cartMsg != null}">
            <tr>
                <td colspan="8"><b>${sessionScope.cartMsg}</b></td>
                <%
                    String cartMsg= (String) session.getAttribute("cartMsg");
                    cartMsg = null;
                    session.setAttribute("cartMsg", cartMsg);
                %>
            </tr>
            </c:if>

            <c:forEach var="cartItem" items="${sessionScope.cart.cartItems}">
            <tr id="order${cartItem.item.itemId}">
                <td>
                    <a href="itemForm?itemId=${cartItem.item.itemId}">${cartItem.item.itemId}</a>

                </td>
                <td>${cartItem.item.attribute1} ${cartItem.item.attribute2}
                        ${cartItem.item.attribute3} ${cartItem.item.attribute4}
                        ${cartItem.item.attribute5} ${cartItem.item.product.name}</td>
                <td id="orderQuantity${cartItem.item.itemId}">
                        ${cartItem.quantity}
                </td>

                <td id="orderTotal${cartItem.item.itemId}">
                    <fmt:formatNumber value="${cartItem.total}" pattern="$#,##0.00" />
                </td>

            </tr>
            </c:forEach>

            <tr>
                <td colspan="8">
                    YOU ADDRESS FOR EACH PRODUCT:
                    <input type="radio" name="address" value="${sessionScope.loginAccount.address1}">${sessionScope.loginAccount.address1}
                    <input type="radio" name="address" value="${sessionScope.loginAccount.address2}">${sessionScope.loginAccount.address2}
                </td>
            </tr>

            <tr>
                <td colspan="7">
                    Sub Total: <fmt:formatNumber
                        value="${sessionScope.cart.subTotal}" pattern="$#,##0.00" />
                    <input type="button" name="save" id="victory" class="orderButton" value="Pay Off">
                </td>
            </tr>
        </table>
    </form>
</div>
<script src="js/ordercheck.js"></script>